/*
 * $Author: isan.wong@gmail.com $
 * $Date: 2011-07-29 01:03:06 +0000 (Fri, 29 Jul 2011) $
 * $Rev: 13 $
 * $URL: http://capitalbio-smd.googlecode.com/svn/src/com/capitalbio/smd/gui/ProjectPane.java $
 */

package com.capitalbio.smd.gui;

import java.util.HashMap;
import java.util.Map;

import javax.swing.JOptionPane;
import javax.swing.JSplitPane;
import javax.swing.ListSelectionModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.biojava.bio.BioException;

import com.capitalbio.smd.base.Project;
import com.capitalbio.smd.base.Sample;

/**
 * 项目面板, 包括左侧的样品列表面板, 和右侧的样品面板
 * 
 * @author Wang Lei
 * 
 */
public class ProjectPane extends JSplitPane {
	private static final long serialVersionUID = 1L;

	protected Log log = LogFactory.getLog(this.getClass());

	private MainFrame mainFrame;
	private SampleListPane sampleListPane;
	private SamplePane samplePane;
	private LoadingPane loadingPane;
	private ListSelectionListener sampleListSelectionListener;
	private Project project;

	private Map<Sample, SamplePane> samplePanes;
	
	public ProjectPane(MainFrame mainFrame) {
		this.mainFrame = mainFrame;
		samplePanes = new HashMap<Sample, SamplePane>();
		sampleListPane = new SampleListPane(this);
		samplePane = new SamplePane(mainFrame);
		loadingPane = new LoadingPane();

		setOrientation(JSplitPane.HORIZONTAL_SPLIT);
		setLeftComponent(sampleListPane);
		setRightComponent(loadingPane);
		setDividerSize(2);
		setDividerLocation(200);
		setResizeWeight(0);
		
		showLoadingPane();

		sampleListSelectionListener = new ListSelectionListener() {
			@Override
			public void valueChanged(ListSelectionEvent evt) {
				if (evt.getValueIsAdjusting()) {
					return;
				}

				ListSelectionModel model = (ListSelectionModel) evt.getSource();
				if (model.isSelectionEmpty()) {
					log.debug("当前没有选择样品");
				} else {
					int i = model.getMinSelectionIndex();
					Sample sample = sampleListPane.getSampleListData()
							.getSampleAt(i);
					/*
					if (samplePanes.containsKey(sample)) {
						samplePane = samplePanes.get(sample);
					}
					else {
						samplePane = new SamplePane();
						samplePane.setSample(sample);
						samplePanes.put(sample, samplePane);
					}
					*/

					try {
						samplePane.setSample(sample);
					} catch (final Exception e) {
						log.error(e);
						JOptionPane.showMessageDialog(ProjectPane.this.mainFrame,
								"Error occured while setting sample.\n" + e.getMessage(),
								"Error Occured", JOptionPane.ERROR_MESSAGE);
					}
					log.debug("Sample index " + i + " was selected:" + sample.getName());
					
					if (getRightComponent() != samplePane) {
						int dividerLocation = getDividerLocation();
						setRightComponent(samplePane);
						setDividerLocation(dividerLocation);
					}
				}
			}
		};
		sampleListPane.getSampleListTable().getSelectionModel()
				.addListSelectionListener(sampleListSelectionListener);
	}

	public void updateProject(Project project) {
		this.project = project;
		//fireTableDataChanged();
		sampleListPane.updateProject(project);
	}
	
	public void showLoadingPane() {
		int dividerLocation = getDividerLocation();
		setRightComponent(loadingPane);
		setDividerLocation(dividerLocation);
	}
	
	public SampleListPane getSampleListPane() {
		return sampleListPane;
	}

	public void setSampleListPane(SampleListPane sampleListPane) {
		this.sampleListPane = sampleListPane;
	}

	public SamplePane getSamplePane() {
		return samplePane;
	}

	public void setSamplePane(SamplePane samplePane) {
		this.samplePane = samplePane;
	}

	public Map<Sample, SamplePane> getSamplePanes() {
		return samplePanes;
	}
	
	public MainFrame getMainFrame() {
		return mainFrame;
	}
}
